/**
 * @author Shine Chen
 * @date 2011.10.28
 */

package org.mini.core
{
	import com.shine.utils.StringUtil;
	import com.swfaddress.SWFAddress;
	
	import flash.net.LocalConnection;
	import flash.system.System;
	import flash.utils.Dictionary;
	
	import org.mini.templates.AbstractPage;
	
	use namespace mini_internal;

	public class SiteStructure
	{
		static public var lock:Boolean=false;
		static public var isStandAlone:Boolean;
		static public var player9:Boolean;
		
		static private var _current_route:String;
		static private var _receptors:Dictionary;
		static private var _site_route:ModuleObject;
		static private var _start_up:Boolean=false;
		
		static mini_internal function setup(xml:XML):void
		{
			_current_route=xml.@id;
			
			var node:XML=xml;
			
			while(node.hasOwnProperty("page")){
				if(!(node.page && node.page[0])) break;
				_current_route+="/"+node.page[0].@id;
				node=node.page[0];
			}
			
			(_site_route ||=new ModuleObject).xml=xml;
			_site_route.line="";
		}
		
		static public function get startUp():Boolean
		{
			return _start_up;
		}
		
		static public function get route():String
		{
			return _current_route;
		}
		
		static mini_internal function addRouteId(id:String,mo:ModuleObject):void
		{
			if(!StringUtil.isEmpty(id) && (_receptors ||=new Dictionary())[id]==undefined) {
				(_receptors ||=new Dictionary())[id]=mo;
			}
		}
		
		static mini_internal function launch():void
		{
			var value:String=SWFAddress.getValue();
			
//			trace("SWFAddress : value =",value);
			
			value=value.substring(1,value.length);
			
			var route:String=(StringUtil.isEmpty(value) || !availableRoute(value)) ? _current_route : value;
			var copy:String=_current_route;
			_current_route="";
			
			if(!goTo(route)){
				if(!goTo(copy)) throw new Error("site launch fail , pls check site xml .");
			}
			
			_start_up=true;
		}
		
		static public function goTo(route:String):Boolean
		{
			if(_current_route==route || lock || !availableRoute(route)) {
				trace("SiteStructure:goTo",route,"fail.");
				return false;
			}
			
//			trace("SiteStructure:goTo",route);
			
			_site_route.route=route;
			_current_route=route;
			
			var mo:ModuleObject=_receptors[format(route)];
			if(mo && !StringUtil.isEmpty(mo.title)) SWFAddress.setTitle(mo.title);
			
			SWFAddress.setValue(_current_route);
			GC();
			
			return true;
		}
		
		static public function getPage(route:String):*
		{
			route=format(route);
			
			if(_receptors[route]!=undefined){
				return (_receptors[route] as ModuleObject).getInstance();
			}
			
			return null; 
		}
		
		static private function availableRoute(path:String):Boolean
		{
			if(_receptors==null || StringUtil.isEmpty(path)) return false;
			return (_receptors[format(path)]!=undefined);
		}
		
		static private function format(path:String):String
		{
			var fail:String=path.charAt(path.length-1);
			path=fail=="/" ? path : (path+"/");
			return path;
		}
		
		static public function GC():void
		{
			if(!player9) System.gc();
			else{
				try {  
					new LocalConnection().connect("author@Shine");
					new LocalConnection().connect("author@Shine"); 
				} 
				catch (e:Error) {};
			}
		}
		
		static mini_internal function dispose():void
		{
			_site_route.route="";
			_site_route=null;
			_receptors=null;
		}
	}
}